package com.liuqing.www.http程序读取;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

//http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/index.html
public class 模拟登录 {
	private static final String path="http://login.sina.com.cn/sso/login.php";
	private static final String username="lq123456789000@sina.cn";
	private static final String password="19871030";
	public static void main(String[] args) {
		login();
	}
	public static void login(){
		HttpClient client=new HttpClient();
		PostMethod httpost=new PostMethod(path);
		httpost.setRequestHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)"); 
		// method.setRequestHeader( " Accept " ,  " */* " );

		      // method.setRequestHeader("Accept-Language", "zh-cn");

		        // method.setRequestHeader("Accept-Encoding", "gzip,deflate");

	    //method.setRequestHeader( " User-Agent " ,  " Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1;) " );

		         // 设置非常重要

		httpost.setRequestHeader( " Connection " ,  " Keep-Alive " );


	    NameValuePair name = new NameValuePair("username",username);     
	    NameValuePair pass = new NameValuePair("password",password); 
	    NameValuePair x1 = new NameValuePair("formhash","e6e527f2"); 
	    NameValuePair x2= new NameValuePair("back_url","http://club.mil.news.sina.com.cn/logging.php?action=login&loginsubmit=1&referer=http%3A%2F%2Fclub.mil.news.sina.com.cn%2Fviewthread.php%3Ftid%3D323247"); 
	    NameValuePair x3= new NameValuePair("service","bbsclub"); 
	    NameValuePair x4= new NameValuePair("entry","bbsclub"); 
	    NameValuePair x5= new NameValuePair("useticket","0"); 
	    NameValuePair x6= new NameValuePair("gateway","1"); 
	    httpost.setRequestBody(new NameValuePair[] {x1,x2,x3,x4,x5,x6,name,pass});
		try {
			System.out.println(client.executeMethod(httpost));
			;
			for(Cookie cook:client.getState().getCookies()){
				System.out.println(cook.getName()+"-->"+cook.getValue());
			}
			InputStream is=httpost.getResponseBodyAsStream();
			BufferedReader br=new BufferedReader(new InputStreamReader(is,"utf8"));
			String bf=null;
			while((bf=br.readLine())!=null){
				System.out.println(bf);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

}
